﻿namespace EasyToolkit.Excel.Application
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    using EasyToolkit.Excel.Application.Async;
    using EasyToolkit.Excel.Extensions;
    using EasyToolkit.Tools.Log;
    using Microsoft.Office.Core;
    using ExcelInterop = Microsoft.Office.Interop.Excel;

    public class ExcelApplication : IDisposable
    {
        #region attribute and properties
        private CommandBarControl newMenu;

        internal ExcelInterop.Application Application
        { get; private set; }

        internal ExcelAsyncExecution ExcelAsyncExecution
        { get; private set; }
        #endregion

        #region .ctors
        public ExcelApplication(ExcelInterop.Application appli)
        {
            Application = appli;
            ExcelAsyncExecution = new ExcelAsyncExecution(this);
            try
            {
                newMenu = Application.CommandBars["Worksheet Menu Bar"].FindControl(1,
                                                                                    18, //the item to look for
                                                                                    Type.Missing, //the tag property (in this case missing)
                                                                                    Type.Missing, //the visible property (in this case missing)
                                                                                    true); //we want to look for it recursively
            }
            catch (Exception ex)
            {
                Logger.Instance.LogExceptionFormat(LogType.Error, ex, "Init 'CheckEditMode' failed.{0}", ex.Message);
            }
        }
        #endregion

        #region public methods
        public bool IsInEditMode()
        {
            if (Application != null && newMenu == null)
                return false;
            return ! newMenu.Enabled;
        }

        public void DisplayException(string message, Exception ex)
        {
            StringBuilder builder = new StringBuilder(message);

            Exception currentEx = ex;
            while (currentEx != null)
            {
                builder.AppendFormat("\n\r{0}", currentEx.Message);
                currentEx = currentEx.InnerException;
            }

            if (Application != null)
            {
                using (ExcelMainWindow mainWindow = new ExcelMainWindow(Application.Hwnd))
                {
                    MessageBox.Show(mainWindow, builder.ToString(), "Excel Presentation Framework", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
                MessageBox.Show(builder.ToString(), "Excel Presentation Framework", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void DisplayMessageBox(string message)
        {
            DisplayMessageBox(message, MessageBoxIcon.Information);
        }


        public void DisplayMessageBox(string message, MessageBoxIcon icon)
        {
            if (Application != null)
            {
                using (ExcelMainWindow mainWindow = new ExcelMainWindow(Application.Hwnd))
                {
                    MessageBox.Show(mainWindow, message, "Excel Presentation Framework", MessageBoxButtons.OK, icon);
                }
            }
            else
                MessageBox.Show(message, "Excel Presentation Framework", MessageBoxButtons.OK, icon);
        }
        #endregion

        public void Dispose()
        {
            if (Application != null)
            {
                Marshal.ReleaseComObject(Application);
                Application = null;
            }
        }
    }
}
